package com.aft.terra.modules.game.model;

import java.util.List;

import com.aft.terra.modules.game.model.base.BaseOnList;
import com.jfinal.plugin.activerecord.Db;

public class OnList extends BaseOnList<OnList> {
	
	private static final long serialVersionUID = 1L;

	public static final OnList dao = new OnList();
	
	/**
	 * 获取时间段的排行榜
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public List<OnList> findAll(String date) {
		StringBuffer sql = new StringBuffer().append(" select * from t_on_list ")
		.append(" where time_quantum = ? order by score_sum desc,modifyDate limit 200");
		return find(sql.toString(),date);
	}

	/**
	 * 根据  时间串来换取 当前的最小积分
	 * @param data 时间串
	 * @return
	 */
	public List<OnList> findMinScore(String data) {
		String sql = "select IFNULL(MIN(score_sum),0) socre,user_id,id from t_on_list ol where time_quantum = ? ";
		return find(sql,data);
	}
	
	/**
	 * 根据时间段来查询有没有这个用户的数据
	 * @param userId 用户id
	 * @param date 时间段
	 * @return
	 */
	public OnList findByUserId(String userId,String date) {
		String sql = "select * from t_on_list where user_id = ? and time_quantum = ?";
		return findFirst(sql,userId,date);
	}

	/**
	 * 更新排行榜中最小数据
	 */
	public void updateMinScore(Long scoreSum,User user) {
		StringBuffer sql = new StringBuffer()
				.append("update t_on_list set ")
				.append("score_sum = ?,user_id = ? ,user_name = ? ,w_headimgurl = ? ")
				.append(",modifyDate =now() ORDER BY score_sum ,modifyDate DESC LIMIT 1");
		Db.update(sql.toString(),scoreSum,user.getId(),user.getUserName(),user.getStr("w_headimgurl"));
	}

	/**
	 * 根据id更新已经在排行榜中的数据
	 */
	public void updateIdOnScore(Long id,Long scoreSum,User user) {
		StringBuffer sql = new StringBuffer()
			.append("update t_on_list set ")
			.append("score_sum = ?,user_id = ? ,user_name = ? ,w_headimgurl = ? ")
			.append(",modifyDate =now() where id = ?");
		Db.update(sql.toString(),scoreSum,user.getId(),user.getUserName(),user.getStr("w_headimgurl"),id);
	}
}
